package org.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Building;
import org.example.form.SearchForm;
import org.example.service.BuildingService;
import org.example.vo.PageVO;
import org.example.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@RestController
@RequestMapping("/building")
public class BuildingController {
  @Autowired
    private BuildingService buildingService;
  @PostMapping("/save")
    public ResultVO save(@RequestBody Building building){
      Boolean result = buildingService.save(building);
      if(!result){
          return ResultVO.error("保存失败");
      }
      return ResultVO.success(null);
  }
  @GetMapping("/list/{page}/{size}")
  public ResultVO list(@PathVariable int page, @PathVariable int size){
     PageVO pageVO = this.buildingService.list(page, size);
     return ResultVO.success(pageVO);
  }
  @GetMapping("/search")
    public ResultVO search(SearchForm searchForm){
    PageVO pageVO = this.buildingService.search(searchForm);
    return ResultVO.success(pageVO);

  }
  @GetMapping("/findById/{id}")
  public ResultVO getById(@PathVariable int id){
    Building building = buildingService.getById(id);
    return ResultVO.success(building);
  }
  @PutMapping("/update")
  public ResultVO update(@RequestBody Building building){
    Boolean result = buildingService.updateById(building);
    if(!result){
        return ResultVO.error("更新失败");
    }
    return ResultVO.success(null);
  }
  @DeleteMapping("/deleteById/{id}")
  public ResultVO deleteById(@PathVariable("id") Integer id){
    Boolean delete = this.buildingService.deleteById(id);
    if(!delete) return ResultVO.error("删除失败");
    return ResultVO.success(null);
  }
  @GetMapping("/list")
  public ResultVO list(){
    List<Building> list = this.buildingService.list();
    return ResultVO.success(list);
  }
}

